<?php
/**
 * Created by IntelliJ IDEA.
 * User: wuzesheng
 * Date: 4/15/14
 * Time: 5:28 PM
 */
ini_set('max_execution_time', '0');
ini_set('memory_limit', '6000M');
require(__DIR__ . "/../bootstrap.php");

// Construct the FDS client
$access_key = "5411744243597";
$access_secret = "hu/XTi7mhl1j+pL5qkQGnw==";
$credential = new \FDS\credential\BasicFDSCredential($access_key, $access_secret);
$fds_config = new \FDS\FDSClientConfiguration();
$fds_config->setRegionName("awsbj0");
$fds_config->enableHttps(true);
$fds_config->enableCdnForUpload(false);
$fds_config->enableCdnForDownload(true);

$fds_client = new \FDS\GalaxyFDSClient($credential, $fds_config);
$bucket_name = "hard";

// Check and create the bucket
if (!$fds_client->doesBucketExist($bucket_name)) {
  $fds_client->createBucket($bucket_name);
}
//exit;
session_start();
$object_name = $_SESSION['filename'];
$object_content = $_SESSION['filecontent'];
//echo('上传文件名:' . $object_name . '</br>');
//echo('上传内容:' . $object_content . '</br>');
$result = $fds_client->putObject($bucket_name, $object_name, $object_content);
$fds_client->setPublic($bucket_name,$object_name);
$download = $fds_client->generateDownloadObjectUri($bucket_name,$object_name);
$_SESSION['download'] = $download;
// Put an object
//$object_name = "xxxxoooo.txt";
//$result = $fds_client->generatePresignedUri($bucket_name, $object_name,147635675400000,$http_methmod = "PUT");
////echo $result;
////exit;
//
////$result = $fds_client->initMultipartUpload($bucket_name, $object_name);
////print_r($result);
////$result = $fds_client->refreshObject($bucket_name, $object_name);
//$result = $fds_client->initMultipartUpload($bucket_name, $object_name);
//$uploadId = $result->getUploadId();
//
//
//$url=$fds_client->generatePresignedUri2($bucket_name, $object_name,147635675400000,$uploadId,1,$http_method = "PUT", $content_type = null);
//echo $url;
////$data = "1111";
////$return = curlrequest($url, $data, 'PUT');
////var_dump($return);
//$url=$fds_client->generatePresignedUri2($bucket_name, $object_name,147635675400000,$uploadId,2,$http_method = "PUT", $content_type = null);
//echo $url;
////$data = "2222";
////$return = curlrequest($url, $data, 'PUT');
////var_dump($return);
//exit;
///*
//$signatures=array();
//for ($i=1;$i<1000;$i++){
//  $url=$fds_client->generatePresignedUri2($bucket_name, $object_name,147635675400000,$uploadId,$i,$http_method = "PUT", $content_type = null);
//  $tmp=explode("Signature=",$url);
//  $signatures[]=$tmp[1];
//}
//print_r($signatures);*/
////echo $url."&uploadId=".$uploadId."&partNumber=1";
//$url = $fds_client->generatePresignedUri3($bucket_name, $object_name,147635675400000,$uploadId,$http_method = "PUT", $content_type = null);
//function curlrequest($url,$data,$method='PUT'){
//    $ch = curl_init(); //初始化CURL句柄
//    curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
//    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串，而不是直接输出
//    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
//
//    curl_setopt($ch,CURLOPT_HTTPHEADER,array("X-HTTP-Method-Override: $method"));//设置HTTP头信息
//    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
//    $document = curl_exec($ch);//执行预定义的CURL
//    if(!curl_errno($ch)){
//      $info = curl_getinfo($ch);
//      echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
//    } else {
//      echo 'Curl error: ' . curl_error($ch);
//    }
//    curl_close($ch);
//
//    return $document;
//}
////$url = 'http://localhost/test/curl.php';
//$data = "";
//$return = curlrequest($url, $data, 'PUT');
//
//var_dump($return);
//exit;
////$object_content = file_get_contents("/mnt/default/v.madv360.com/php/uploads/files/".$object_name);
////$result = $fds_client->putObject($bucket_name, $object_name, $object_content);
////print_r($result);
////x-xiaomi-acl 	用于指定所创建的Bucket的ACL信息 值:PUBLIC_READ | PUBLIC_WRITE | AUTHENTICATED_READ | AUTHENTICATED_WRITE
////echo $fds_client->getGrantList();
////$fds_client->setPublic($bucket_name,$object_name);
//// Check the object existence
////$exist = $fds_client->doesObjectExist($bucket_name, $object_name);
////echo $exist;
////assert($exist);
////echo $exist;
//
//// Get the object and check content
////$object = $fds_client->getObject($bucket_name, $object_name);
////$content = $object->getObjectContent();
////print_r($content);
//
//// Delete the object
////$fds_client->deleteObject($bucket_name, $object_name);
//
//// Delete the bucket
////$fds_client->deleteBucket($bucket_name);
//
//// Check bucket existence
////$exist = $fds_client->doesBucketExist($bucket_name);
////assert(!$exist);
////public function testMultipartUpload() {
////self::$PARAM = func_get_args();
////self::$BUCKET_NAME = "fds-bvt-".strtolower(self::$PARAM[0]. "php-testMultipartUpload". self::$NAME_FIX . self::getMillisecond());
//use FDS\model\UploadPartResult;
//$fdsClient = $fds_client;
//
////$totalParts = 10;
////$partDataArray = array();
////for ($i = 0; $i < $totalParts; ++$i) {
//  //array_push($partDataArray, $i . $i);
////}
//
//$result = $fdsClient->initMultipartUpload($bucket_name, $object_name);
//$uploadId = $result->getUploadId();
//$uploadPartResultArray = array();
//
//$handle = fopen("/mnt/default/v.madv360.com/php/uploads/files/".$object_name, "rb");
//if (FALSE === $handle) {
//    exit("Failed to open stream to URL");
//}
//$i=1;
//while (!feof($handle)) {
//    $contents = fread($handle, 10000000);
//    //echo $contents;
//    $uploadPartResult = $fdsClient->uploadPart($bucket_name,$object_name, $uploadId, $i++, $contents);
//    array_push($uploadPartResultArray, $uploadPartResult);
//}
//fclose($handle);
//
////for ($i = 1; $i < $totalParts; ++$i) {
//  //$uploadPartResult = $fdsClient->uploadPart($bucket_name,$object_name, $uploadId, $i, $partDataArray[$i]);
//  //array_push($uploadPartResultArray, $uploadPartResult);
////}
//$uploadPartResultList = new \FDS\model\UploadPartResultList();
//$uploadPartResultList->setUploadPartResultList($uploadPartResultArray);
//$fdsClient->completeMultipartUpload($bucket_name, $object_name,$uploadId, null, $uploadPartResultList);
//
//$exist = $fds_client->doesObjectExist($bucket_name, $object_name);
//echo $exist;
////$expectObjectContent = join('', $partDataArray);
////$object = $fdsClient->getObject(self::$BUCKET_NAME, self::$PROJECT_NAME);
////$actualObjectContent = $object->getObjectContent();
////$this->assertEquals($expectObjectContent, $actualObjectContent);
////}
